<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  var arr = function (candidates, target) {
    var res = [];
    candidates = candidates.sort();
    function dfs(i, sum, ans) {
      if (sum > target) return;
      if (sum == target) {
        res.push([...ans]);
      }
      for (i; i < candidates.length; i++) {
        sum += candidates[i];
        ans.push(candidates[i]);
        dfs(i + 1, sum, ans);
        sum -= candidates[i];
        ans.pop();
        while (candidates[i + 1] == candidates[i]) i++;
      }
    }
    dfs(0, 0, []);
    return res;
  };
  console.log(arr(candidates = [10, 1, 2, 7, 6, 1, 5], target = 8,));
</script>

</html>